package com.example.fw;

import java.util.Properties;

import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.JFrameOperator;


public class ApplicationManager {
	
	private static ApplicationManager instance;
	private Properties properties;
	private FolderHelper folderHelper;
	private JFrameOperator mainFrame;
	private MenuHelper menuHelper;
	
	private ApplicationManager(){}
	
	public static ApplicationManager getInstance(){
		if (instance == null){
			instance = new ApplicationManager();
		}
		return instance;
	}

	public String getProperty(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}
	
	public String getProperty(String key) {
		return properties.getProperty(key);
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public void stop() {
		getApplication().requestClose();
	}

	public FolderHelper getFolderHelper() {
		if (folderHelper == null){
			folderHelper = new FolderHelper(this);
		}
		return folderHelper;
	}

	public JFrameOperator getApplication() {
		if (mainFrame == null){
			try {
				new ClassReference(this.getProperty("classReference")).startApplication();
				mainFrame = new JFrameOperator(this.getProperty("mainFrame"));
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		return mainFrame;		
	}

	public MenuHelper getMenuHelper() {
		if (menuHelper == null){
			menuHelper = new MenuHelper(this);
		}
		return menuHelper;
	}

}
